package com.neusoft.neu24.nep.bootaop.demos.interceptor;

import com.neusoft.neu24.nep.bootaop.demos.entity.CommonResult;
import com.neusoft.neu24.nep.bootaop.demos.entity.TodayNews;
import com.neusoft.neu24.nep.bootaop.demos.web.BasicController;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import reactor.core.publisher.Mono;
// Springboot 3
@Component
public class MyInterceptor implements HandlerInterceptor {

    @Autowired
    WebClient  webClient;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        Mono<TodayNews> result;
//        result = (Mono<TodayNews>) webClient.get()
//                .uri("/toutiaohot/index?key=c65cff44e82fc3b1d27622e80e0a1b6c")
//                .accept(MediaType.APPLICATION_JSON)
//                .retrieve()
//                .bodyToMono(TodayNews.class)
//                .doOnNext(todayNews -> {
//                    System.out.println(todayNews);
//                })
//                .onErrorResume(e -> {
//                    System.out.println("Failed to fetch today's news  : " +   e);
//            // 根据实际情况，这里可以返回一个空的Mono或者一个包含默认值的Mono
//            return Mono.empty();
//        })
//                .onErrorReturn(new TodayNews());
//
//        if (result.block().getCode() == 200) {
//            System.out.println("preHandle...");
//            return true;
//        } else {
////            response.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
//            return false;
//        }




//        System.out.println(System.currentTimeMillis());
//        TodayNews todayNews = result.block();
//        System.out.println(System.currentTimeMillis());

        //        Mono<TodayNews> mono =  webClient.get()
//                .uri("/toutiaohot/index?key=c65cff44e82fc3b1d27622e80e0a1b6c")
//                .retrieve()
//                .bodyToMono(TodayNews.class);
//

//        webClient.wait(10001l);
//              阻塞式调用
        CommonResult result = webClient.get()
                .uri("/toutiaohot/index?key=c65cff44e82fc3b1d27622e80e0a1b6c")
                .retrieve()
                .bodyToMono(CommonResult.class)
                .block();
        System.out.println(result);
        System.out.println(  request.getHeader("User-Agent"));
        System.out.println("preHandle...");
        return System.currentTimeMillis() % 2 == 0;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
//
//        assert handler instanceof HandlerMethod;
//        HandlerMethod  method = (HandlerMethod) handler;
//        System.out.println( method.getMethod()  );
        System.out.println("postHandle...");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion...");
    }
}
